//
//  ViewController2.m
//  数据存储
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()
//学生数组
@property(nonatomic,strong) NSMutableArray *stuList;

@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;



@end

@implementation ViewController2

//重写getter方法 懒加载

-(NSMutableArray *)stuList
{
    if (_stuList==nil) {
        NSFileManager *fm=[NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]]) {
            _stuList=[NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else{
            _stuList=[NSMutableArray array];
        }
    }
    return _stuList;
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
}
//plist文件储存位置
- (NSString *)plistPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    //    NSLog(@"存储位置:%@",filePath);
    return filePath;
}


/** 保存数据到plist(覆盖以前的数据) */
-(void)savePlist
{
    [self.stuList writeToFile:[self plistPath] atomically:YES];
    
}
//增加学生
-(IBAction)add2:(id)sender
{
    NSMutableDictionary *dict=[@{@"stuId":self.idTextField.text,
                                 @"stuName":self.nameTextField.text,
                                 @"stuAge":self.ageTextField.text

                                 } mutableCopy ];
    
    [self.stuList addObject:dict];
    [self savePlist];
    
    
}

// 删除学生
- (IBAction)remove2:(id)sender
{
    // 1. 遍历学生数组
    for(NSMutableDictionary *dict in self.stuList)
    {
        // 2. 如果学号相同
        if([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            // 3. 将该学生这个字典从数组中移除
            [self.stuList removeObject:dict];
            break;
        }
    }
    // 4. 数组保存一下
    [self savePlist];
}

// 修改学生
- (IBAction)modify2:(id)sender
{
    // 1. 遍历学生数组
    for(NSMutableDictionary *dict in self.stuList)
    {
        // 2. 如果学号相同
        if([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            // 3. 修改该学生的信息
            dict[@"stuName"] = self.nameTextField.text;
            dict[@"stuAge"] = self.ageTextField.text;
            break;
        }
    }
    // 4. 数组保存一下
    [self savePlist];
}

// 根据学号查询学生
- (IBAction)find2:(id)sender
{
    // 1. 遍历学生数组
    for(NSMutableDictionary *dict in self.stuList)
    {
        // 2. 如果学号相同
        if([dict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            // 3. 将找到的这个学生信息显示到界面上
            self.nameTextField.text = dict[@"stuName"];
            self.ageTextField.text = dict[@"stuAge"];
            break;
        }
    }
}

- (IBAction)findAll:(id)sender
{
    // 1. 遍历学生数组，显示一下
    for(NSMutableDictionary *dict in self.stuList)
    {
        NSLog(@"%@ %@ %@",dict[@"stuId"],dict[@"stuName"],dict[@"stuAge"]);
    }
}






@end
